package com.hoshiicloud.user.service.impl.company;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.company.AttachmentType;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.company.IAttachmentTypeMapper;
import com.hoshiicloud.user.service.company.IAttachmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 附件类型 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class AttachmentTypeServiceImpl extends ServiceImpl<IAttachmentTypeMapper, AttachmentType> implements IAttachmentTypeService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    private IAttachmentTypeMapper attachmentTypeMapper;
    /**
     * @param id
     * @return AttachmentType
     * @description: 根据id查询附件类型信息
     */
    @Cacheable(cacheNames = "attachmenttype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public AttachmentType getById(Long id) {
        return baseMapper.selectById(id);
    }
    /**
     * 根据id组查询附件类型信息
     * @param ids    主键ID列表
     * @return List<AttachmentType>
     */
    @Cacheable(cacheNames = "attachmenttype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "租户管理-附件类型管理-根据id查询附件类型信息")
    @Override
    public List<AttachmentType> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }
    /**
     * 新增附件类型
     * @param entity 新增参数
     * @return Long 主键ID
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Logable(authName = "租户管理-附件类型管理-根据id查询附件类型信息")
    @Override
    public Long insert(AttachmentType entity) {
        entity.setCreatedTime(LocalDateTime.now());
        // entity.setModifiedTime(LocalDateTime.now());
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");
        // entity.setModifiedBy("admin");
        entity.setUpdatedBy("admin");
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        return entity.getId();
    }
    /**
     * @param entity
     * @return boolean
     * @description: 修改附件类型信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Logable(authName = "租户管理-附件类型管理-根据id查询附件类型信息")
    @Override
    public Long update(AttachmentType entity) {
        // entity.setModifiedTime(LocalDateTime.now());
        entity.setUpdatedTime(LocalDateTime.now());
        // entity.setModifiedBy("admin1");
        entity.setUpdatedBy("admin1");
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改附件类型失败"));
        }
        return entity.getId();
    }

    /**
     * @param
     * @return boolean
     * @description: 新增附件类型
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Override
    public boolean save(AttachmentType entity) {

        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        // entity.setModifiedTime(LocalDateTime.now());
        entity.setUpdatedTime(LocalDateTime.now());
        int row = 0;
        try {
            row = baseMapper.insert(entity);
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改附件类型信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Override
    public boolean updateById(AttachmentType entity) {

        int row = 0;
        try {
            // entity.setModifiedTime(LocalDateTime.now());
            entity.setUpdatedTime(LocalDateTime.now());
            row = baseMapper.updateById(entity);
        } catch (ServiceException e) {
            e.getError();
        }catch (Exception e) {
            e.getMessage();
        }
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改附件类型失败"));
        }
        return true;
    }


    /**
     * 删除附件类型信息(逻辑删除)
     * @param id ID
     * @return boolean
     * @throws ServiceException
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Logable(authName = "租户管理-附件类型管理-删除附件类型信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        AttachmentType entity = this.getById(id);
        entity.setEnabled(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除附件类型信息失败"));
        } else {
            return true;
        }
    }


    /**
     *根据附件类型信息进行分页查询
     * @param page
     * @param queryWrapper
     * @param key
     * @return
     */
    @Override
    @Cacheable(cacheNames = "attachmenttype",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "根据附件类型信息进行分页查询")
    public IPage<AttachmentType> getPage(IPage<AttachmentType> page, QueryWrapper<AttachmentType> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Logable(authName = "根据类目Id与供应商类型Id查询附件信息")
    public List<AttachmentType> selectAttachmentTypeByCategoryIdOrOrganizationTypeId(List<Long> categoryIds, Long organizationTypeId) {
        return attachmentTypeMapper.selectAttachmentTypeByCategoryIdOrOrganizationTypeId(categoryIds,organizationTypeId);
    }
}
